$(function() {
	var pager;
	var show;

	var urlJson = "goNewRequestResetPassJson.json";
	showTable(urlJson);
});

// Load Request Reset Json
function showTable(string) {
	$("#tablepaging > tbody").html("");
	$
			.getJSON(
					string,
					{
						ajax : 'true'
					},
					function(data) {
						var length = data.length;
						var color = "";
						var username = "";
						var row = 0;

						if (length == 0) {
							var content = '<tr>';
							content += '<td colspan="8" style="text-align:center;">';
							content += 'No matching Request found';
							content += '</td>';
							content += '</tr>';
							$("#tablepaging > tbody").append(content);
						} else {

							for (var i = 0; i < length; i++) {
								row++;
								if (data[i].midName == null) {
									data[i].midName = "";
								}
								var content = "";
								content = '<tr>'
								content += '<td class="cell-title" style="text-align: center">'
										+ row;
								content += '<input type="hidden" id="transactionID'
										+ row
										+ '" value="'
										+ data[i].transactionID + '">';
								content += '</td>';
								content += '<td class="cell-title" style="text-align: center">'
										+ data[i].username + '</td>';
								content += '<td class="cell-title" style="text-align: center">'
										+ data[i].role + '</td>';
								content += '<td class="cell-title" style="text-align: center">'
										+ data[i].firstName
										+ " "
										+ data[i].midName
										+ " "
										+ data[i].lastName + '</td>';
								content += '<td class="cell-time" style="text-align: center">'
										+ data[i].date + '</td>';
								content += '<td class="cell-time" style="text-align: center">';
								content += '<button id="btnShowResetPassword'
										+ row
										+ '" type="button" class="btn btn-group btn-primary">Process</button>';
								content += '</td>';
								content += '</tr>';

								$("#tablepaging > tbody").append(content);
							}
						}
						document.getElementById("numberOfRow").value = row;

						// Init paging
						// Set number of row per pages
						pager = new Pager('tablepaging', 15);
						pager.init();
						pager.showPageNav('pager', 'pageNavPosition');
						pager.showPage(1);
						// End - Init paging

						show = new ActionShow();
					});
}
// End - Load Request Reset Json

// Show detailed Registered User
function ShowDetail(event, row) {
	event.preventDefault();

	var transactionID = $("#transactionID" + row).val();
	var show;
	// Start Load Detail
	jQuery
			.ajax({
				url : 'transactionDetailJson/' + transactionID + '.json',
				method : 'POST',
				contentType : 'application/json',
				data : $('#btnShowResetPassword' + row).serialize()
			})
			.done(
					function(data) {
						// Do something with the response
						var length = data.length;
						$("#detailContent").html("");

						var content = '';
						if (data.accImpact.accountMidName == null) {
							data.accImpact.accountMidName = "";
						}
						
						content += '<div class="module-head">';
						content += '<h3>Reset Password</h3>';
						content += '</div>';
						content += '<input type="hidden" name="txtTransactionID" value="'
								+ data.transactionId + '">';
						content += '<div style="width: 220px; margin-left: 30px;margin-top: 15px;">';
						// Load Username
						content += '<label>Username: </label>';
						content += '<div class="controls row-fluid">';
						content += '<input class="span12" type="text" readonly="true" value="'
								+ data.accImpact.accountLoginId
								+ '" name="txtRequestUsername">';
						content += '</div></div>';
						// End - Load Username

						// Load Full Name
						content += '<div style="width: 220px; margin-left: 30px;">';
						content += '<label>Full Name: </label>';
						content += '<div class="controls row-fluid">';
						content += '<input class="span12" type="text" readonly="true" value="'
								+ data.accImpact.accountFirstName
								+ " "
								+ data.accImpact.accountMidName
								+ " "
								+ data.accImpact.accountLastName + '">';
						content += '</div></div>';
						// End - Load Full Name

						// Reason for reset password
						content += '<div class="" style="margin-left: 30px;">';
						content += '<label class="control-label" for="">Reason:</label>';
						content += '<div class="controls">';
						content += '<textarea style="resize: none; width: 466px;text-align:left;" readonly="true">'
								+ data.command + '</textarea>';
						content += '</div></div>';
						// End - Reason for reset password

						// Message
						// End - Message

						content += '<div style="margin-top: 15px; margin-bottom: 30px">';
						content += '<div class="controls clearfix">';
						content += '<button type="submit" class="btn btn-primary pull-left" style="margin-left: 15px;" name="action" value="reset">Reset Password</button>';
						content += '<button type="submit" class="btn btn-primary pull-left" style="margin-left: 15px;" name="action" value="reject">Reject</button>';
						content += '<button id="btnHideResetPassword" type="button" class="btn btn-primary pull-left" style="margin-left: 15px;">Cancel</button>';
						content += '</div></div>';

						$("#detailContent").append(content);
						show = new ActionShow();
					}).fail(function() {
				// Whoops; show an error.
				alert("failed to load transaction detail");
			});

	// End - Start Load Detail
};
// End - Show detailed Registered User

function ActionShow() {
	// Show Reset Password Panel
	function generate_ShowProfileFandler(row) {
		return function(event) {
			show = new ShowDetail(event, row);
			$("#panelAdvanceSearch").slideUp("slow");
			$("#panelResetPassword").slideDown("slow");
			$('#panelReason').slideUp("slow")
		};
	}

	function generate_HideProfileFandler(row) {
		return function(event) {
			$("#panelResetPassword").slideUp("slow");
		};
	}

	numberOfRow = $("#numberOfRow").val();

	for (var int = 1; int <= numberOfRow; int++) {
		var row = int.toString();

		$('#btnShowResetPassword' + row)
				.click(generate_ShowProfileFandler(row));

		$('#btnHideResetPassword').click(generate_HideProfileFandler(row));
	}
	// End - Show Reset Password Panel
}

$(document).ready(
		function() {
			// Advance Search
			$("#btnShowAdvanceSearch").click(function() {
				$("#panelResetPassword").slideUp("slow");
				$("#panelAdvanceSearch").slideDown("slow");
			});

			$("#btnHideAdvanceSearch").click(function() {
				$("#panelAdvanceSearch").slideUp("slow");
			});
			// End - Advance Search

			$("#btnAdvancedSearch").click(
					function() {
						var pager;
						var show;

						// Get value from advanced search panel
						var fullName = $('#txtSFullName').val();
						var username = $('#txtSUsername').val();
						var role = $('#txtSRole').val();
						var from = $('#txtSFrom').val();
						var to = $('#txtSTo').val();
						// End - Get value from advanced search panel

						if (fullName == "") {
							fullName = "null";
						}
						if (username == "") {
							username = "null";
						}
						if (role == "") {
							role = "null";
						}
						if (from == "") {
							from = "null";
						}
						if (to == "") {
							to = "null";
						}

						var urlJson = "advancedSearchNewRequest/" + fullName
								+ "/" + username + "/" + role + "/" + from
								+ "/" + to + ".json";

						$.ajax({
							type : "GET",
							url : urlJson,
							success : function(response) {
								showTable(urlJson);
							},
						});

					});
		});